<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryform" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="f0602">
        <el-input v-model="queryParams.f0602" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门" prop="f0617">
        <el-select v-model="queryParams.f0501" clearable placeholder="请选择部门" @keyup.enter.native="handleQuery">
          <el-option v-for="item in deptList" :key="item.f0501" :label="item.f0502" :value="item.f0501">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="f0610">
        <el-select v-model="queryParams.f0610" clearable @keyup.enter.native="handleQuery" placeholder="请选择">
          <el-option label="在职" :value="1"></el-option>
          <el-option label="离职" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleDistribute">分配token</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['AccountInformation:Account:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['AccountInformation:Account:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['AccountInformation:Account:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['AccountInformation:Account:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AccountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="f0602" />
      <el-table-column label="用户名" align="center" prop="f0603" />
      <el-table-column label="电话" align="center" prop="f0604" />
      <el-table-column label="消耗token数" align="center" prop="f0605" />
      <el-table-column label="部门" align="center" prop="f0617" />
      <el-table-column label="状态" align="center" prop="f0610">
        <template slot-scope="scope">
          <el-tag :type="scope.row.f0610 === 1 ? 'success' : 'danger'">{{ scope.row.f0610 === 1 ? '在职' : '离职'
            }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="剩余token数" align="center" prop="f0611" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改账户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门" prop="f0617">
          <el-select style="width: 100%;" v-model="form.f0617" placeholder="请选择部门" @keyup.enter.native="handleQuery">
            <el-option v-for="item in deptList" :key="item.f0501" :label="item.f0502" :value="item.f0502">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="f0602">
          <el-input v-model="form.f0602" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="用户名" prop="f0603">
          <el-input v-model="form.f0603" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="f0604">
          <el-input v-model="form.f0604" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="状态" prop="f0610">
          <el-select style="width: 100%;" v-model="form.f0610" clearable placeholder="请选择">
            <el-option label="在职" :value="1"></el-option>
            <el-option label="离职" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitform">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="分配token" :visible.sync="tokenOpen" width="500px" append-to-body>
      <el-input v-model="tokenNum" placeholder="请输入分配token数"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitToken">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccount, getAccountMessage, delAccount, addAccount, updateUser, updateToken } from "@/api/AccountInformation/Account";
import { listDepartmentManage } from "@/api/departmentManage/departmentManage";
import { number } from "echarts";

export default {
  name: "Account",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账户表格数据
      AccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      tokenOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        f0501: null,
        f0601: null,
        f0602: null,
        f0603: null,
        f0604: null,
        f0605: null,
        f0606: null,
        f0607: null,
        f0609: null,
        f0610: null,
        f0611: null,
        f0612: null,
        f0614: null,
        f0615: null,
        f0617: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //部门列表
      deptList: [],
      tokenNum: ''
    };
  },
  created() {
    this.getList();
    listDepartmentManage().then(res => {
      this.deptList = res.rows
    })
  },
  methods: {
    /** 查询账户列表 */
    getList() {
      this.loading = true;
      listAccount(this.queryParams).then(response => {
        this.AccountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection)
      this.ids = selection.map(item => item.f0601)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 分配token按钮
    handleDistribute() {
      if(this.ids.length == 0) {
        this.$modal.msgError("请选择要分配token的账户")
        return
      }
      this.tokenOpen = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.tokenOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.tokenNum = ''
      this.form = {
        f0501: null,
        f0601: null,
        f0602: null,
        f0603: null,
        f0604: null,
        f0605: null,
        f0606: null,
        f0607: null,
        f0609: null,
        f0610: null,
        f0611: null,
        f0612: null,
        f0614: null,
        f0615: null,
        f0617: null
      };
    },
    resetform() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        f0501: null,
        f0601: null,
        f0602: null,
        f0603: null,
        f0604: null,
        f0605: null,
        f0606: null,
        f0607: null,
        f0609: null,
        f0610: null,
        f0611: null,
        f0612: null,
        f0614: null,
        f0615: null,
        f0617: null
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetform("queryform");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加账户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const f0601 = row.f0601 || this.ids
      getAccountMessage(f0601).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改账户";
      });
    },
    /** 提交按钮 */
    submitform() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.f0601 != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitToken() {
      if (this.tokenNum == '') {
        this.$modal.msgError("请输入token数量");
        return
      }
      const data = {
        F0601s: this.ids,
        sendNumber: this.tokenNum
      }
      updateToken(data).then(response => {
        this.tokenOpen = false;
        this.reset();
        this.getList();
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const f0601s = row.f0601 || this.ids;
      const name = row.f0602
      this.$modal.confirm('是否确认删除账户编号为"' + name + '"的数据项？').then(function() {
        return delAccount(f0601s);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('AccountInformation/Account/export', {
        ...this.queryParams
      }, `Account_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
